//
//  WOChecklistViewController.m
//
//  Created by Olof Millberg on 2010-03-25.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "WOChecklistViewController.h"
#import "WOChecklistPoint.h"
#import "FinishCell.h"

@implementation WOChecklistViewController

#pragma mark Initialization Methods

/* Gets an instance of the class */
- (id) init
{
	if (self = [super initWithStyle:UITableViewStyleGrouped])
	{
		self.title = @"Checklista";
		UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Checklista" 
															  image:[UIImage imageNamed:@"tabbar_checklist.png"] 
																tag:5];
		self.tabBarItem = theItem;
		[theItem release];
	}
	return self;
}

- (void) viewDidAppear:(BOOL)animated
{
	[[NSUserDefaults standardUserDefaults] setInteger:4 forKey:@"currentTab"];
	self.tableView.rowHeight = 54;
	self.tableView.sectionHeaderHeight = 4;
	self.tableView.sectionFooterHeight = 4;
}

#pragma mark -

#pragma mark UITableView Methods

/* Number of sections this view */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView 
{ 
	return 2;
}

/* Number of rows per section */
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section 
{
	if (section == 0)
		return [self.workOrder.checklistPoints count];
	else
		return 1;
}

/* Fill the cells with content */
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	// Retrieve or create a cell.
	UITableViewCell *cell;
	if (indexPath.section == 0)
	{
		UITableViewCellStyle style =  UITableViewCellStyleDefault;
		cell = [aTableView dequeueReusableCellWithIdentifier:@"BaseCell"];
		if (!cell)
		{
			cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:@"BaseCell"] autorelease];
			cell.selectionStyle = UITableViewCellSelectionStyleNone;
			cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
			cell.textLabel.numberOfLines = 2;
			cell.textLabel.font = [UIFont  boldSystemFontOfSize:15.f];
			[cell.imageView setContentMode:UIViewContentModeScaleAspectFill];
		}
		
		WOChecklistPoint *selectedPoint = [self.workOrder.checklistPoints objectAtIndex:indexPath.row];
		
		cell.textLabel.text = selectedPoint.txt;
		
		//Set correct image.
		if(selectedPoint.checked)
			cell.imageView.image = [UIImage imageNamed:@"checkedBox.png"];
		else
			cell.imageView.image = [UIImage imageNamed:@"uncheckedBox.png"];
	}
	else
	{
		cell = [aTableView dequeueReusableCellWithIdentifier:@"FinishCell"];
		if (!cell)
		{
			NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"FinishCell" owner:self options:nil];
				
			for (id currentObject in topLevelObjects) 
			{
				if ([currentObject isKindOfClass:[UITableViewCell class]])
					cell = currentObject;
				break;
			}
		}
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		finishButton = ((FinishCell*)cell).finishButton;
		[finishButton addTarget:self action:@selector(finishWorkOrder) forControlEvents:UIControlEventTouchUpInside];
		finishButton.enabled = [self allPointsChecked];
	}
	return cell;
}

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{	
	//Make sure the right section is tapped.
	if (indexPath.section > 0)
		return;
		
	WOChecklistPoint *selectedPoint = [self.workOrder.checklistPoints objectAtIndex:indexPath.row];
	UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
	
	//Invert the checked boolean.
	selectedPoint.checked = !selectedPoint.checked;
	
	//Set correct image.
	if(selectedPoint.checked)
		cell.imageView.image = [UIImage imageNamed:@"checkedBox.png"];
	else
		cell.imageView.image = [UIImage imageNamed:@"uncheckedBox.png"];
	
	finishButton.enabled = [self allPointsChecked];
}

#pragma mark -

#pragma mark Self Made Methods

- (BOOL) allPointsChecked
{
	BOOL allChecked = YES;
	for (int i = 0; i < [self.workOrder.checklistPoints count]; i++)
	{
		WOChecklistPoint *currentPoint = [self.workOrder.checklistPoints objectAtIndex:i];
		allChecked = allChecked && currentPoint.checked;
	}
	return allChecked;
}

- (void) finishWorkOrder
{
	//This assumes "Finished" is the last object in the status list.
	workOrder.status = [workOrder.statusTypes lastObject];
	workOrder.endDate = [NSDate date];
	[self.tabBarController.navigationController popViewControllerAnimated:YES];
}

#pragma mark -

@end